﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.FallNursingMeasuresToPrevent
{
    public partial class PrintFallNursingMeasuresToPrevent : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        DateTime Start;
        DateTime End;
        #endregion

        #region 构造函数
        public PrintFallNursingMeasuresToPrevent(PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            Start = StartDate;
            End = EndDate;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\预防住院患儿跌倒集束化护理措施.grf");
            //设置与数据源的连接串，因为在设计时指定的路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //链接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(PatientInfo.PATIENT_ID, PatientInfo.VISIT_ID, (int)FormType.FallMTP, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDateTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                if (list == null || list.Count <= 0) return;
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                FallMTP new_ps = XmlUtil.XMLToObject<FallMTP>(list[0].NursingFormData, Encoding.UTF8);
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord rd = list[i];
                    FallMTP mtp = XmlUtil.XMLToObject<FallMTP>(rd.NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["PatientName"] = rd.PatientName;   //PatientName
                    row["SEX"] = PatientInfo.SEX;  //SEX
                    row["AGE"] = PatientInfo.AGE;   // AGE
                    row["NURSE_CELL_NAME"] = PatientInfo.NURSE_CELL_NAME;  //NURSE_CELL_NAME
                    row["DEPARTMENT_NAME"] = PatientInfo.DEPARTMENT_NAME;  //DEPARTMENT_NAME
                    row["BED_NO"] = PatientInfo.BED_NO;    //BED_NO
                    row["PatientId"] = PatientInfo.PATIENT_ID;  //PatientId
                    row["hulicuoshi1"] = mtp.HuLiCuoShi.Split('#')[0].Equals("1") ? 1 : 0;
                    row["hulicuoshi2"] = mtp.HuLiCuoShi.Split('#')[1].Equals("1") ? 1 : 0;
                    row["hulicuoshi3"] = mtp.HuLiCuoShi.Split('#')[2].Equals("1") ? 1 : 0;
                    row["hulicuoshi4"] = mtp.HuLiCuoShi.Split('#')[3].Equals("1") ? 1 : 0;
                    row["hulicuoshi5"] = mtp.HuLiCuoShi.Split('#')[4].Equals("1") ? 1 : 0;
                    row["hulicuoshi6"] = mtp.HuLiCuoShi.Split('#')[5].Equals("1") ? 1 : 0;
                    row["hulicuoshi7"] = mtp.HuLiCuoShi.Split('#')[6].Equals("1") ? 1 : 0;
                    row["hulicuoshi8"] = mtp.HuLiCuoShi.Split('#')[7].Equals("1") ? 1 : 0;
                    row["hulicuoshi9"] = mtp.HuLiCuoShi.Split('#')[8].Equals("1") ? 1 : 0;
                    row["hulicuoshi10"] = mtp.HuLiCuoShi.Split('#')[9].Equals("1") ? 1 : 0;
                    row["hulicuoshi11"] = mtp.HuLiCuoShi.Split('#')[10].Equals("1") ? 1 : 0;
                    row["hulicuoshi12"] = mtp.HuLiCuoShi.Split('#')[11].Equals("1") ? 1 : 0;
                    row["hulicuoshi13"] = mtp.HuLiCuoShi.Split('#')[12].Equals("1") ? 1 : 0;
                    row["hulicuoshi14"] = mtp.HuLiCuoShi.Split('#')[13].Equals("1") ? 1 : 0;
                    row["hulicuoshi15"] = mtp.HuLiCuoShi.Split('#')[14].Equals("1") ? 1 : 0;
                    row["hulicuoshi16"] = mtp.HuLiCuoShi.Split('#')[15].Equals("1") ? 1 : 0;
                    row["hulicuoshi17"] = mtp.HuLiCuoShi.Split('#')[16].Equals("1") ? 1 : 0;
                    row["hulicuoshi18"] = mtp.HuLiCuoShi.Split('#')[17].Equals("1") ? 1 : 0;
                    row["hulicuoshi19"] = mtp.HuLiCuoShi.Split('#')[18].Equals("1") ? 1 : 0;
                    row["hulicuoshi20"] = mtp.HuLiCuoShi.Split('#')[19].Equals("1") ? 1 : 0;
                    row["AssessmentDate"] = mtp.RiQi;
                    row["diedaopingfen"] = mtp.DieDaoPingFen;
                    dt.Rows.Add(row);
                }
                //将 datatablle 的数据转储到 Grid++Report 的数据集中
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDateTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        private static void CreateDateTableHead(DataTable dt)
        {
            dt.Columns.Add("PatientName", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PatientId", typeof(string));
            dt.Columns.Add("hulicuoshi1", typeof(string));
            dt.Columns.Add("hulicuoshi2", typeof(string));
            dt.Columns.Add("hulicuoshi3", typeof(string));
            dt.Columns.Add("hulicuoshi4", typeof(string));
            dt.Columns.Add("hulicuoshi5", typeof(string));
            dt.Columns.Add("hulicuoshi6", typeof(string));
            dt.Columns.Add("hulicuoshi7", typeof(string));
            dt.Columns.Add("hulicuoshi8", typeof(string));
            dt.Columns.Add("hulicuoshi9", typeof(string));
            dt.Columns.Add("hulicuoshi10", typeof(string));
            dt.Columns.Add("hulicuoshi11", typeof(string));
            dt.Columns.Add("hulicuoshi12", typeof(string));
            dt.Columns.Add("hulicuoshi13", typeof(string));
            dt.Columns.Add("hulicuoshi14", typeof(string));
            dt.Columns.Add("hulicuoshi15", typeof(string));
            dt.Columns.Add("hulicuoshi16", typeof(string));
            dt.Columns.Add("hulicuoshi17", typeof(string));
            dt.Columns.Add("hulicuoshi18", typeof(string));
            dt.Columns.Add("hulicuoshi19", typeof(string));
            dt.Columns.Add("hulicuoshi20", typeof(string));
            dt.Columns.Add("AssessmentDate", typeof(string));
            dt.Columns.Add("diedaopingfen", typeof(string));
        }

        #region 窗体加载启动锐浪报表
        private void PrintFallNursingMeasuresToPrevent_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}